import argparse
import yaml
import os
from flask_app import app
from dotenv import load_dotenv

from scripts.ssc.data_processing import process_ssc_data
from scripts.ssc.model_training import train_ssc_model
from scripts.ssc.prediction import predict_ssc_model
from utils.web_scrapers.ssc_scraper import scrape_ssc_data
# from utils.web_scrapers.ssq_scraper import scrape_ssq_data
# from utils.web_scrapers.zc_scraper import scrape_zc_data
# from utils.web_scrapers.qxc_scraper import scrape_qxc_data
# from utils.excel_converters.ssq_excel_converter import convert_ssq_excel_to_csv
# from utils.excel_converters.zc_excel_converter import convert_zc_excel_to_csv
# from utils.excel_converters.qxc_excel_converter import convert_qxc_excel_to_csv
# from scripts.ssq.model_training import train_ssq_model
# from scripts.zc.model_training import train_zc_model
# from scripts.qxc.model_training import train_qxc_model
# from scripts.ssq.prediction import predict_ssq
# from scripts.zc.prediction import predict_zc
# from scripts.qxc.prediction import predict_qxc

load_dotenv()

os.environ["PROJECT_PATH"] = os.path.dirname(os.path.abspath(__file__))


def load_config(config_path):
    with open(config_path, 'r') as f:
        return yaml.safe_load(f)


def main():
    parser = argparse.ArgumentParser(description="Lottery Prediction System")

    parser.add_argument("action", type=str, choices=["scrape", "convert", "process", "train", "predict", "compare"],
                        help="Action to perform: scrape, convert, process, train, predict, compare")

    parser.add_argument("--lottery", type=str, choices=["ssc","ssq", "zc", "qxc"], required=False,
                        help="Lottery type: ssc,ssq, zc, qxc")

    args = parser.parse_args()

    config_map = {
        "ssc": "config/ssc_config.yaml",  # 时时彩
        "ssq": "config/ssq_config.yaml",  # 双色球
        "zc": "config/zc_config.yaml",  # 足彩
        "qxc": "config/qxc_config.yaml"  # 七星彩
    }

    config = load_config(config_map[args.lottery])

    if args.action == "scrape":
        if args.lottery == "ssc":
            scrape_ssc_data(config['scraper']['url'], config['scraper']['output_file'])
        # elif args.lottery == "ssq":
        #     scrape_ssq_data(config['web_scraper_url'], config['data_path'])
        # elif args.lottery == "zc":
        #     scrape_zc_data(config['web_scraper_url'], config['data_path'])
        # elif args.lottery == "qxc":
        #     scrape_qxc_data(config['web_scraper_url'], config['data_path'])

    elif args.action == "convert":
        pass
        # if args.lottery == "ssc":
        #     convert_ssq_excel_to_csv(config['excel']['file_path'], config['excel']['sheet_name'], config['excel']['output_file'])
        # if args.lottery == "ssq":
        #     convert_ssq_excel_to_csv(config['excel_file_path'], config['excel_sheet_name'], config['data_path'])
        # elif args.lottery == "zc":
        #     convert_zc_excel_to_csv(config['excel_file_path'], config['excel_sheet_name'], config['data_path'])
        # elif args.lottery == "qxc":
        #     convert_qxc_excel_to_csv(config['excel_file_path'], config['excel_sheet_name'], config['data_path'])

    if args.action == "process":
        if args.lottery == "ssc":
            process_ssc_data(config_map[args.lottery])
        # elif args.lottery == "ssq":
        #     process_ssq_data()

    elif args.action == "train":
        if args.lottery == "ssc":
            train_ssc_model()
        # elif args.lottery == "ssq":
        #     train_ssq_model()
        # elif args.lottery == "zc":
        #     train_zc_model()
        # elif args.lottery == "qxc":
        #     train_qxc_model()

    elif args.action == "predict":
        if args.lottery == "ssc":
             y_pred_rf, rf_accuracy = predict_ssc_model('knn')
             print(y_pred_rf)
             print(rf_accuracy)
        # elif args.lottery == "ssq":
        #     predict_ssq()
        # elif args.lottery == "zc":
        #     predict_zc()
        # elif args.lottery == "qxc":
        #     predict_qxc()

    elif args.action == "compare":
        # 横向对比模型的逻辑
        print("模型横向对比尚未实现")

    elif args.action == "serve":
        app.run(debug=True, host="0.0.0.0", port=5000)


if __name__ == "__main__":
    # python main.py scrape --lottery ssq：爬取双色球数据。
    # python main.py convert --lottery ssq：转换双色球的Excel文件为CSV。
    # python main.py process --lottery ssc： 如果有网络链接，则优先抓取网络数据，保存为csv，并清理数据保存为处理数据，否则转换excel数据位csv数据，并进行清洗保存为处理数据。
    # python main.py train --lottery ssq：训练双色球模型。
    # python main.py predict --lottery ssq：进行双色球预测。
    main()
